package cn.cau.control;

import cn.cau.entity.*;
import cn.cau.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class QrcodeController {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private RuleCategoryService ruleCategoryService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BasicEnumService basicEnumService;
    @Autowired
    private NonEnumService nonEnumService;
    @Autowired
    private QrcodeService qrcodeService;
    @RequestMapping(value = "/QrcodeRuleList",method= RequestMethod.GET)
    public ModelAndView showCodeRuleList(){
        List<CodeRule> codeRules=null;
        codeRules = codeRuleService.findCodeRuleAll();
        List<RuleCategory> ruleCategories=null;
        ruleCategories=ruleCategoryService.findAllRuleCategorys();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("ruleCategories",ruleCategories);
        modelAndView.addObject("codeRules",codeRules);
        modelAndView.setViewName("/qrCode/codeRuleList");
        return modelAndView;
    }
    @RequestMapping(value = "/codeRuleInfo",method= RequestMethod.GET)
    public ModelAndView RedirToNext(@RequestParam(value = "id")int id){
        CodeRule c1=codeRuleService.findCodeRuleById(id);
        ModelAndView modelAndView = new ModelAndView();
        int count=c1.getCodeCodeCount();
        List<BasicEnumSum> enumsum=basicEnumService.findBasicEnumSumByCodeRuleId(id);
        List<NonEnum> nonenumsum=nonEnumService.findNonEnumByCodeId(id);
        modelAndView.addObject("count",count);
        modelAndView.addObject("rule",c1);
        modelAndView.addObject("enumsum",enumsum);
        modelAndView.addObject("nonenumsum",nonenumsum);
        modelAndView.setViewName("/qrCode/codeRuleInfo");
        return modelAndView;
    }
    @RequestMapping(value = "/qrCodeResult",method= RequestMethod.POST)
    public ModelAndView CodeGenerate(HttpServletRequest request,
                                     HttpSession session,
                                     @RequestParam(value = "categoryOptions")List<String> categoryOptions,
                                     @RequestParam(value = "categoryOptionsDetails")List<String> categoryOptionsDetails,
                                     @RequestParam(value = "codeRuleId")int codeRuleId
    ){
        CodeRule codeRule=codeRuleService.findCodeRuleById(codeRuleId);
        String ruleCategoryName=ruleCategoryService.findAllRuleCategorys().get(codeRule.getRuleCategoryId()).getRuleCategoryName();
        String categoryName=categoryService.findCategoryById(codeRule.getCategoryId()).getCategory();
        String userName="未登录用户";
        if (session.getAttribute("userInfo") != null) {
            //如果用户登录
            CommonUser commonUser = (CommonUser) session.getAttribute("userInfo");
            userName=commonUser.getUserName();
        }
        String projectPath="D:\\Code\\JavaProjects\\SENM\\";
        Qrcode qrcode=qrcodeService.generateQrcode(codeRule,ruleCategoryName,categoryName,categoryOptionsDetails,categoryOptions,userName,projectPath);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("qrCodePath",qrcode.getPath());
        modelAndView.addObject("count",codeRule.getCodeCodeCount());
        modelAndView.addObject("rule",codeRule);
        modelAndView.addObject("enumsum",basicEnumService.findBasicEnumSumByCodeRuleId(codeRuleId));
        modelAndView.addObject("nonenumsum",nonEnumService.findNonEnumByCodeId(codeRuleId));
        modelAndView.setViewName("/qrCode/codeRuleInfo");
        return modelAndView;
    }
}
